// pages/person/index.js
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        headTop: "",
        headHeight: "",
        sumHeight: "",
        is_user_show: true,
        is_user: 0,
        userUrl: "",
        userName: "",
        balance: "0.00", //余额
        coupon: 0, //优惠券
        pricecoupon: 0,
        cardcoupon:0,
        integral: "0.00", //积分
        showUser: false,
        mobi: '',
        version: '版本号：v1.0.9.3',
        serviceList: [{
                name: "客服",
                url: "/image/customer.png",
            },
            {
                name: "兑换记录",
                url: "/image/exchange.png",
                link: "/pages/person/record/index",
            },
            {
                name: "收货地址",
                url: "/image/address.png",
                link: "/pages/person/address/index",
            },
            {
                name: "设置",
                url: "/image/setting.png",
                link: "/pages/person/setting/index",
            },
        ],
    },
    onLoad() {
        const accountInfo = wx.getAccountInfoSync();
        const version = accountInfo.miniProgram.version;

        const that = this;
        that.setData({
            version: version ? '版本号：' + version : that.version,
        });
        if (wx.getStorageSync("token")) {
            const token = wx.getStorageSync("token")

            app.http.post(app.api.userInfo, {
                token
            }, (res) => {
                let mobi = res.data.mobile.substr(0, 3) + "****" + res.data.mobile.substr(7, 4)
                that.setData({
                    userUrl: res.data.avatar,
                    userName: res.data.nickname,
                    balance: res.data.balance,
                    coupon: res.data.coupon,
                    pricecoupon: res.data.pricecoupon,
                    cardcoupon:res.data.cardcoupon,
                    integral: res.data.integral,
                    mobi,
                    showUser: true,

                });
                wx.setStorageSync("mobile", res.data.mobile)
            })
        }
        that.setData({
            headTop: app.globalData.headTop,
            headHeight: app.globalData.headHeight,
            sumHeight: app.globalData.sumHeight,

        });
    },
    onShow() {
        if (typeof this.getTabBar === "function" && this.getTabBar()) {
            console.log("设置选中项 2");
            this.getTabBar().setData({
                selected: 4,
            });
        }
    },
    //   登录
    login() {
        if (wx.getStorageSync('token') == "") {
            this.setData({
                is_user_show: false,
                is_user: 1
            });
        } else if (wx.getStorageSync('mobile') == "") {
            this.setData({
                is_user_show: false,
                is_user: 2
            });
        }
    },
    // 登录遮罩层
    close(e) {
        this.setData({
            is_user_show: e.detail,
            is_user: 0
        });
    },
    // 登陆后更新
    renew(e) {
        let mobi = e.detail.mobile.substr(0, 3) + "****" + e.detail.mobile.substr(7, 4)
        this.setData({
            userUrl: e.detail.avatar,
            userName: e.detail.nickname,
            balance: e.detail.balance,
            coupon: e.detail.coupon,
            integral: e.detail.integral,
            showUser: true,
            mobi,
        });
        wx.setStorageSync('userInfo', e.detail)
        wx.setStorageSync("mobile", e.detail.mobile)

    },
    // 开通会员
    vip() {
        const that = this
        if (wx.getStorageSync('mobile') == "") {
            that.login()
            return
        }
        wx.navigateTo({
            url: "/pages/person/vipplas/index",
        });
    },
    // 积分
    linkTointegra() {
        const that = this
        if (wx.getStorageSync('mobile') == "") {
            that.login()
            return
        }
        console.log(this.data.integral)
        wx.navigateTo({
            url: "/pages/integral/recordex/index?integral=" + that.data.integral
        });
    },
    // 余额
    linkTocoinex() {
        const that = this
        if (wx.getStorageSync('mobile') == "") {
            that.login()
            return
        }
        wx.navigateTo({
            url: "/pages/person/coinex/index?money=" + this.data.balance,
        });
    },
    // 我的服务跳转
    linkTo(e) {
        const that = this
        const url = e.currentTarget.dataset.link;
        if (url == "/pages/person/setting/index") {
            console.log(1)
            if (wx.getStorageSync('token') == "") {
                that.login()
                return
            }
            wx.navigateTo({
                url,
            });
            return
        }
        if (wx.getStorageSync('mobile') == "") {
            that.login()
            return
        }
        wx.navigateTo({
            url,
        });
    },
    // 优惠券
    linkTopres() {
        const that = this
        if (wx.getStorageSync('mobile') == "") {
            that.login()
            return
        }
        wx.navigateTo({
            url: "/pages/person/maincoupon/index",
        });
    },

    // 团购券
    linkToprescoupon() {
        const that = this
        if (wx.getStorageSync('mobile') == "") {
            that.login()
            return
        }

        wx.navigateTo({
            url: "/pages/person/mainpricecoupon/index",
        });
    },

    // 团购券
    linkTopcardcoupon() {
        const that = this
        if (wx.getStorageSync('mobile') == "") {
            that.login()
            return
        }

        wx.navigateTo({
            url: "/pages/person/cardcoupon/index",
        });
    },
    onShareAppMessage() {

    },
    onShareTimeline() {}
});